﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：PurchaseOrderAdd_Step1.cs
    // 文件功能描述：新建采购单，选择医疗机构
    // 创建标识：吕海乐2009-04-27
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class PurchaseOrderAdd_Step1 : BasePage
    {
        public PurchaseOrderAdd_Step1()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.新建采购单.ToString();
        }

        IBLL_Hospital ih = Factory.BLL_Hospital();
        IBLL_Area ia = Factory.BLL_Area();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // BindDrop(); 
                BindHospital();

                //此处调用目的，为了设置DropdownList的状态
                GetDefaultAreaString();
            }
        }

        /// <summary>
        /// 绑定医疗机构
        /// </summary>
        private void BindHospital()
        {
            int totalCount = 0;
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            IList<Hospital> hospitalList = ih.GetHospitalByWhere(new HZ.DrugTrade.Model.Query.Hospital() { AreaID = GetAreaID(), SortFields = "HospitalID ASC" }, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwHospital.PageSize = AspNetPager1.PageSize;
            gvwHospital.DataSource = hospitalList;
            gvwHospital.DataBind();
        }

        #region 绑定地区信息
        /// <summary>
        /// 获取地区查询条件 AreaID
        /// </summary>
        /// <returns></returns>
        private string GetAreaID()
        {
            string areaId = "";

            //首次进入使用当前用户所在区域ID
            areaId = CurrentAccount.GetUser().Org.Area.AreaID;

            //优先获取区县，其次城市，再次省份
            if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]).String != Constants.ALL_ITEM_VALUE)
            {
                areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"];
            }
            else
            {
                if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlCity"];
                }
                else if (new Common.ConvertHelper(Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"]).String != Constants.ALL_ITEM_VALUE)
                {
                    areaId = Request.Form["ctl00$ContentPlaceHolder1$ddlProvice"];
                }

            }
            return areaId;
        }
        /// <summary>
        /// 获取所有省级区域信息
        /// </summary>
        private void DropProvice()
        {
            IList<Area> proviceList = ia.GetAllProvince();
            ddlProvice.DataSource = proviceList;
            ddlProvice.DataTextField = "AreaName";
            ddlProvice.DataValueField = "AreaID";
            ddlProvice.DataBind();
            ListItem item = new ListItem();
            item.Text = Constants.ALL_ITEM_TEXT;
            item.Value = Constants.ALL_ITEM_VALUE;
            ddlProvice.Items.Insert(0, item);
        }

        /// <summary>
        /// 根据省级编号获取所有市
        /// </summary>
        /// <param name="ProvinceAreaID"></param>
        private void DropCity()
        {
            ddlCity.Items.Clear();
            ListItem item = new ListItem();
            item.Text = Constants.ALL_ITEM_TEXT;
            item.Value = Constants.ALL_ITEM_VALUE;
            ddlCity.Items.Insert(0, item);
        }

        /// <summary>
        /// 根据市级编号获取所有县区
        /// </summary>
        /// <param name="CityAreaID"></param>
        private void DropCounty()
        {
            ddlCountry.Items.Clear();
            ListItem item = new ListItem();
            item.Text = Constants.ALL_ITEM_TEXT;
            item.Value = Constants.ALL_ITEM_VALUE;
            ddlCountry.Items.Insert(0, item);
        }

        private void BindDrop()
        {
            DropProvice();
            DropCity();
            DropCounty();
        }

        /// <summary>
        /// 根据省信息绑定市信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlProvice_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlProvice.SelectedItem.Value == Constants.ALL_ITEM_VALUE)
            {
                DropCity();
                DropCounty();
                return;
            }
            string ProvinceAreaID = ddlProvice.SelectedItem.Value;
            IList<Area> cityList = ia.GetAllCity(ProvinceAreaID);
            if (cityList.Count > 0)
            {
                ddlCity.DataSource = cityList;
                ddlCity.DataTextField = "AreaName";
                ddlCity.DataValueField = "AreaID";
                ddlCity.DataBind();
                ListItem item = new ListItem();
                item.Text = Constants.ALL_ITEM_TEXT;
                item.Value = Constants.ALL_ITEM_VALUE;
                ddlCity.Items.Insert(0, item);
                ddlCity.Focus();
                DropCounty();
            }
            else
            {
                DropCity();
                DropCounty();
            }
        }

        /// <summary>
        /// 根据市信息绑定区县信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCity.SelectedItem.Value == Constants.ALL_ITEM_VALUE)
            {
                DropCounty();
                return;
            }
            string CityAreaID = ddlCity.SelectedItem.Value;
            IList<Area> countyList = ia.GetAllCounty(CityAreaID);
            if (countyList.Count > 0)
            {
                ddlCountry.DataSource = countyList;
                ddlCountry.DataTextField = "AreaName";
                ddlCountry.DataValueField = "AreaID";
                ddlCountry.DataBind();
                ListItem item = new ListItem();
                item.Text = Constants.ALL_ITEM_TEXT;
                item.Value = Constants.ALL_ITEM_VALUE;
                ddlCountry.Items.Insert(0, item);
                ddlCountry.Focus();
            }
            else
            {
                DropCounty();
            }
        }
        #endregion
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindHospital();
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindHospital();
        }

        /// <summary>
        /// 点击下一步，将医疗机构ID和医疗机构名称传入下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNext_Click(object sender, EventArgs e)
        {
            string hospitalInfo = hfdHospitalInfo.Value;
            if (!string.IsNullOrEmpty(hospitalInfo))
            {
                string[] arrary = hospitalInfo.Split(',');
                string hID = arrary[0];
                string hName = arrary[1];
                Response.Redirect("PurchaseOrderAdd_Step2.aspx?hID=" + hID + "&hName=" + hName + "");
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何医疗机构！",this);
            }
        }

        /// <summary>
        /// 获取默认选中的地区编号串
        /// 供JS函数使用
        /// </summary>
        /// <returns></returns>
        protected string GetDefaultAreaString()
        {
            //默认城市 江苏省 320000
            string defaultProvince = "320000";
            string defaultCity = "";
            //
            string strArea = "";

            //获取当前用户所在区域
            IBLL_Area areaCtrl = Factory.BLL_Area();
            Area area = areaCtrl.GetArea(CurrentAccount.GetUser().Org.Area.AreaID);

            //如果是市级中心机构，则默认选中具体城市
            if (area.FatherAreaID != "0")
            {
                defaultProvince = area.FatherAreaID;
                defaultCity = area.AreaID;
            }
            else
            {
                defaultProvince = area.AreaID;
            }

            //显示默认地区，范围之外的内容禁止选择
            strArea += "," + "\"" + defaultProvince + "\"";
            this.ddlProvice.Enabled = false;

            if (!string.IsNullOrEmpty(defaultCity))
            {
                strArea += "," + "\"" + defaultCity + "\"";
                this.ddlCity.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCity"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCity"] + "\"";
            }

            if (!string.IsNullOrEmpty(Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"]))
            {
                strArea += "," + "\"" + Request.Form["ctl00$ContentPlaceHolder1$ddlCountry"] + "\"";
            }

            return strArea;
        }
    }
}
